myApp.onPageInit("weekAgenda", function (page) {
    console.log("onPageInit " + page.name);
    var container = $$(page.container).find('.calendar-inline-container'),
        calendarInline,
        ul = $$(page.container).find('.list-block ul'),
        data;

    function loadDayData(p) {
        var date = new Date(p.value[0]);
        var key = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();

        ul.children('li:not(.list-group-title)').remove();
        if (data && data[key]) {
            $$.each(data[key], function (index, value) {
                value.roomName = value.roomName.replace(/会议室/, "");  //省去“会议室”几个字
                var html = Template7.templates.weekAgendaTemplate(value);
                ul.append(html);
            });
        }
    }

    function loadWeekData(p) {
        var day = new Date(p.value[0]);
        var offset = day.getDay() - p.params.firstDay;
        var beginDate = new Date(day.setDate(day.getDate()-offset));
        var endDate = new Date(day.setDate(day.getDate()+6));

        data = {};
        ul.children('li:not(.list-group-title)').remove();
        p.container.find('.picker-calendar-month-current .picker-calendar-day').addClass('picker-calendar-day-disabled');

        $$.getJSONP(SERVER_PATH + 'getWeekDetailListJson.do', {
            startDate: beginDate.Format('yyyy-MM-dd'),
            endDate: endDate.Format('yyyy-MM-dd')
        }, function (json) {
            if (json.code == 1 && json.results.length > 0) {
                $$.each(json.results[0].weekDayList, function (index, value) {
                    var ms = Date.parse(value.date),
                        date = new Date(ms);
                    value.date = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
                    data[value.date] = value.meetingList;

                    console.log('current data contains: ' + value.date);
                });

                var first = true;
                p.container.find('.picker-calendar-month-current .picker-calendar-day').each(function () {
                    var el = $$(this),
                        date = el.data('date');
                    if (data[date]) {
                        el.removeClass('picker-calendar-day-disabled');
                        if (first) {
                            first = false;
                            var day = new Date(parseInt(el.data('year')), parseInt(el.data('month')), parseInt(el.data('day')));
                            p.setValue([day.getTime()]);
                        }
                    }
                });
            }
        });
    }

    var today = new Date(),
        calendarArg = {
            container: container,
            value: [today.getTime()],
            monthNames: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
            dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
            firstDay: 0,
            //showCurrentWeekOnly: false,
            currentYear: today.getFullYear(),
            currentMonth: today.getMonth(),
            currentDay: today.getDate(),
            toolbarTemplate: '<div class="toolbar calendar-custom-toolbar">' +
                                '<div class="toolbar-inner">' +
                                    '<div class="left">' +
                                        '<a href="#" class="link icon-only"><i class="icon icon-back"></i></a>' +
                                    '</div>' +
                                    '<div class="center"></div>' +
                                    '<div class="right">' +
                                        '<a href="#" class="link icon-only"><i class="icon icon-forward"></i></a>' +
                                    '</div>' +
                                '</div>' +
                            '</div>',
            onOpen: function (p) {
                p.container.find('.calendar-custom-toolbar .center').text(p.currentYear + ' 年 ' + p.params.monthNames[p.currentMonth] + ' 月');
                redisplayMonthsWhenShowCurrentWeekOnly(p);
                if (p.params.showCurrentWeekOnly) {
                    p.container.addClass('calendar-week');
                }
                else {
                    p.container.removeClass('calendar-week');
                }
                p.container.find('.calendar-custom-toolbar .left .link').on('click', function () {
                    calendarInline.prevMonth();
                });
                p.container.find('.calendar-custom-toolbar .right .link').on('click', function () {
                    calendarInline.nextMonth();
                });
            },
            onMonthYearChangeStart: function (p) {
                p.container.find('.calendar-custom-toolbar .center').text(p.currentYear + ' 年 ' + p.params.monthNames[p.currentMonth] + ' 月');
                redisplayMonthsWhenShowCurrentWeekOnly(p);
            },
            onChange: function (p, value) {
                //console.log('current ' + p.currentDay + ' ' + p.currentMonth + ' ' + p.currentDay);
                var date = new Date(value[value.length-1]);
                p.currentYear = date.getFullYear();
                p.currentMonth = date.getMonth();
                p.currentDay = date.getDate();

                loadDayData(p);
            },
            onMonthYearChangeEnd: function (p, year, month) {
                var day;
                if (p.params.showCurrentWeekOnly) {
                    var dateCurr = new Date(p.currentYear, p.currentMonth, p.currentDay);
                    var offset = dateCurr.getDay() - p.params.firstDay;
                    var datePrev = new Date(dateCurr.setDate(dateCurr.getDate()-offset));
                    var dateNext = new Date(dateCurr.setDate(dateCurr.getDate()+6));
                    if ((datePrev.getTime() <= today.getTime()) &&
                        (dateNext.getTime() >= today.getTime())) { //currentDay与today在同一行，则选择today
                        day = [today.getTime()];
                    }
                    else { //currentDay与today不在同一行，则选择currentDay所在行的第一天
                        day = [datePrev.getTime()];
                    }
                }
                else {
                    if ((p.currentYear === today.getFullYear()) &&
                        (p.currentMonth === today.getMonth())) { //currentMonth与today相同，则选择today
                        day = [today.getTime()];
                    }
                    else { //currentMonth与today不同，则选择currentMonth的第一天
                        day = [new Date(p.currentYear, p.currentMonth, 1).getTime()];
                    }
                }
                p.setValue(day);
                loadWeekData(p);
            }
    };

    function redisplayMonthsWhenShowCurrentWeekOnly(p) {
        if (p.params.showCurrentWeekOnly) {
            var dates = p.multiDisplayMonthes();
            if (dates.length >= 2) {
                var year1 = dates[0].getFullYear();
                var month1 = dates[0].getMonth();
                var year2 = dates[1].getFullYear();
                var month2 = dates[1].getMonth();
                if (year1 == year2) {
                    p.container.find('.calendar-custom-toolbar .center').text(year1 + ' 年 ' + p.params.monthNames[month1] + ' 月 / ' + p.params.monthNames[month2] + ' 月');
                }
                else {
                    p.container.find('.calendar-custom-toolbar .center').text(year1 + ' 年 ' + p.params.monthNames[month1] + ' 月 / ' + year2 + ' 年 ' + p.params.monthNames[month2] + ' 月');
                }
            }
        }
    }

    function changeCalendarWithShowWeek(showCurrentWeekOnly) {
        if (showCurrentWeekOnly == calendarArg.showCurrentWeekOnly) {
            return;
        }
        if (calendarInline) {
            calendarArg.currentYear = calendarInline.currentYear;
            calendarArg.currentMonth = calendarInline.currentMonth;
            calendarArg.currentDay = calendarInline.currentDay;
            calendarArg.value = [new Date(calendarInline.currentYear, calendarInline.currentMonth, calendarInline.currentDay).getTime()];
            calendarInline.destroy();
            container.html('');
        }
        calendarArg.showCurrentWeekOnly = showCurrentWeekOnly;
        calendarInline = myApp.calendar(calendarArg);
    }
    changeCalendarWithShowWeek(true);

    loadWeekData(calendarInline);

    $$(page.navbarInnerContainer).find('.right .link').click(function () {
        if (data) {
            myApp.template7Data.weekAgendaList = data;
            page.view.router.loadPage('weekAgendaItem-details.html');
        }
    });

});

myApp.onPageInit("weekAgendaItem-details", function (page) {
    console.log("onPageInit " + page.name);
    var ul = $$(page.container).find('.list-block ul');

    $$.each(myApp.template7Data.weekAgendaList, function (date, array) {
        $$.each(array, function (index, value) {
            value.roomName = value.roomName.replace(/会议室/, "");  //省去“会议室”几个字
            var html = Template7.templates.weekAgendaTemplate(value);
            ul.append(html);
        });
    });
});